package com.ruoyi.examination.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.examination.domain.*;
import com.ruoyi.examination.dto.ExamRecordDto;
import com.ruoyi.examination.dto.ExamRecordInfoDto;
import com.ruoyi.examination.mapper.*;
import com.ruoyi.examination.util.SnowflakeIdUtils;
import com.ruoyi.examination.vo.ExamRecordInfoVo;
import com.ruoyi.examination.vo.ExamRecordSelectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.examination.service.IExamRecordService;

/**
 * 考试记录Service业务层处理
 * 
 * @author hcc
 * @date 2025-10-21
 */
@Service
public class ExamRecordServiceImpl implements IExamRecordService 
{
    @Autowired
    private ExamRecordMapper examRecordMapper;

    @Autowired
    private PaperQuestionRelationMapper paperQuestionRelationMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionOptionMapper questionOptionMapper;

    @Autowired
    private AnswerRecordMapper answerRecordMapper;

    /**
     * 查询考试记录
     * 
     * @param examRecordId 考试记录主键
     * @return 考试记录
     */
    @Override
    public ExamRecord selectExamRecordByExamRecordId(String examRecordId)
    {
        return examRecordMapper.selectExamRecordByExamRecordId(examRecordId);
    }

    /**
     * 查询考试记录列表
     * 
     * @param examRecord 考试记录
     * @return 考试记录
     */
    @Override
    public List<ExamRecord> selectExamRecordList(ExamRecord examRecord)
    {
        return examRecordMapper.selectExamRecordList(examRecord);
    }

    /**
     * 新增考试记录
     * 
     * @param examRecord 考试记录
     * @return 结果
     */
    @Override
    public int insertExamRecord(ExamRecord examRecord)
    {
        examRecord.setExamRecordId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        examRecord.setCreateTime(DateUtils.getNowDate());
        return examRecordMapper.insertExamRecord(examRecord);
    }

    /**
     * 修改考试记录
     * 
     * @param examRecord 考试记录
     * @return 结果
     */
    @Override
    public int updateExamRecord(ExamRecord examRecord)
    {
        examRecord.setUpdateTime(DateUtils.getNowDate());
        return examRecordMapper.updateExamRecord(examRecord);
    }

    /**
     * 批量删除考试记录
     * 
     * @param examRecordIds 需要删除的考试记录主键
     * @return 结果
     */
    @Override
    public int deleteExamRecordByExamRecordIds(String[] examRecordIds)
    {
        return examRecordMapper.deleteExamRecordByExamRecordIds(examRecordIds);
    }

    /**
     * 删除考试记录信息
     * 
     * @param examRecordId 考试记录主键
     * @return 结果
     */
    @Override
    public int deleteExamRecordByExamRecordId(String examRecordId)
    {
        return examRecordMapper.deleteExamRecordByExamRecordId(examRecordId);
    }

    /**
     * 查询考试列表
     * @param examRecordSelectVo
     * @return
     */
    @Override
    public List<ExamRecordDto> getExamList(ExamRecordSelectVo examRecordSelectVo) {
        List<ExamRecordDto> examRecordList = examRecordMapper.selectExamList(examRecordSelectVo);
        return examRecordList;
    }

    /**
     * 查询班级考试记录列表
     * lqx
     */
    @Override
    public List<ExamRecordDto> getExamListByClassId(ExamRecordSelectVo examRecordSelectVo) {
        return examRecordMapper.getExamListByClassId(examRecordSelectVo);
    }

    /**
     * 获取考试记录详细信息
     */
    @Override
    public List<ExamRecord> getExamRecordInfo(ExamRecordInfoVo examRecordInfoVo) {
        List<ExamRecord> examRecordList = examRecordMapper.getExamRecordInfo(examRecordInfoVo.getTrainingClassId(),examRecordInfoVo.getStudentId());
        if (StringUtils.isNotEmpty(examRecordList)){
            for (ExamRecord examRecord : examRecordList) {
                String examPaperId = examRecord.getExamPaperId();
                List<PaperQuestionRelation> paperQuestionRelations = paperQuestionRelationMapper.selectPaperQuestionRelationByPaperId(examPaperId);
                if (paperQuestionRelations == null || paperQuestionRelations.isEmpty()) {
                    continue; // 无题目时跳过
                }
                List<ExamRecordInfoDto> dtoList = new ArrayList<>();
                examRecord.setExamRecordInfoDtoList(dtoList);
                for (PaperQuestionRelation paperQuestionRelation : paperQuestionRelations) {
                    // 查询问题
                    Question question = questionMapper.selectQuestionByQuestionId(paperQuestionRelation.getQuestionId());
                    // 查询问题选项
                    List<QuestionOption> questionOptionList = questionOptionMapper.selectQuestionOptionByQuestionId(paperQuestionRelation.getQuestionId());
                    // 查询考生答案
                    AnswerRecord answerRecord = answerRecordMapper.selectAnswerRecordByCondition(examRecord.getExamPaperId(),paperQuestionRelation.getQuestionId() ,examRecordInfoVo.getStudentId(), examRecord.getExamRecordId());
                    ExamRecordInfoDto examRecordInfoDto = new ExamRecordInfoDto();
                    BeanUtils.copyProperties(question,examRecordInfoDto);
                    examRecordInfoDto.setQuestionOptionList(questionOptionList);
                    if (answerRecord != null){
                        examRecordInfoDto.setStudentAnswer(answerRecord.getAnswer());
                        examRecordInfoDto.setAnswerType(answerRecord.getAnswerType());
                    }else {
                        examRecordInfoDto.setAnswerType("2");
                    }
                    dtoList.add(examRecordInfoDto);
                }
            }
        }
        return examRecordList;
    }
}
